﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using iver.Entity;
using DBEntity;

namespace iver.Controllers
{
    public class GuestbookController : ApiController
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns>jsonstr</returns>
        [HttpPost]
        public ResResult GuestbookList([FromBody]ReqList req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = (from u in db.apGuestbook
                        orderby u.id descending
                        select u);
            var arr = list.Skip(req.limit * (req.page - 1)).Take(req.limit);
            int count = list.Count();
            return new ResResult
            {
                success = true,
                result = new
                {
                    count,
                    arr,
                }
            };
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ResResult GuestbookEdit([FromBody]apGuestbook req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            apGuestbook model = db.apGuestbook.FirstOrDefault(u => u.id == req.id);
            if (model == null)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            model.nickName = req.nickName == null ? model.nickName : req.nickName;
            model.message = req.message == null ? model.message : req.message;
            model.temp1 = req.temp1 == null ? model.temp1 : req.temp1;
            model.temp2 = req.temp2 == null ? model.temp2 : req.temp2;
            model.temp3 = req.temp3 == null ? model.temp3 : req.temp3;
            model.replyName = req.replyName == null ? model.replyName : req.replyName;
            model.replyMessage = req.replyMessage == null ? model.replyMessage : req.replyMessage;
            model.isEnable = req.isEnable == null ? model.isEnable : req.isEnable;
            model.orderNum = req.orderNum == null ? model.orderNum : req.orderNum;
            model.remark = req.remark == null ? model.remark : req.remark;
            model.updateTime = DateTime.Now;
            int result = db.SaveChanges();
            Help.Log(authInfo, req.id.ToString());
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessEdit,
                result = new
                {
                    changeCount = result,
                },
            };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="req">id数组</param>
        /// <returns></returns>
        [HttpPost]
        public ResResult GuestbookDel([FromBody]ReqID<List<int>> req)
        {
            AuthInfo authInfo;
            if (!ReqBase.isAuth(out authInfo))
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailAuth,
                };
            }
            iverDB db = new iverDB();
            var list = db.apGuestbook.Where(u => req.id.Contains(u.id));
            int delCount = list.Count();
            if (delCount == 0)
            {
                return new ResResult
                {
                    success = false,
                    rescode = ResCode.FailParams,
                };
            }
            db.apGuestbook.RemoveRange(list);
            db.SaveChanges();
            Help.Log(authInfo, Newtonsoft.Json.JsonConvert.SerializeObject(req.id));
            return new ResResult
            {
                success = true,
                rescode = ResCode.SuccessDel,
                result = new
                {
                    delCount,
                },
            };
        }
    }
}